package com.codedancer.campus.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.po.DynamicComment;
import com.codedancer.campus.qo.dynamic.DynamicCommentPageSearchQo;

public interface DynamicCommentMapper extends BaseMapper<DynamicComment> {

    /**
     * 分页查询动态父级评论
     *
     * @param dynamicCommentPage
     * @param dynamicCommentPageSearchQo
     * @return
     */
    default IPage<DynamicComment> pageParentComment(Page<DynamicComment> dynamicCommentPage,
        DynamicCommentPageSearchQo dynamicCommentPageSearchQo) {
        LambdaQueryWrapper<DynamicComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DynamicComment::getParentUuid, "0");
        lambdaQueryWrapper
            .eq(DynamicComment::getDynamicUuid, dynamicCommentPageSearchQo.getDynamicUuid());
        lambdaQueryWrapper.orderByDesc(DynamicComment::getCreateTime);
        return selectPage(dynamicCommentPage, lambdaQueryWrapper);
    }

    /**
     * 判断是否存在儿子
     *
     * @param uuid
     * @return
     */
    default Boolean hasChildren(String uuid) {
        LambdaQueryWrapper<DynamicComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DynamicComment::getParentUuid, uuid);
        return selectCount(lambdaQueryWrapper) > 0;
    }

    /**
     * 分页查询该评论下子集评论
     *
     * @param dynamicCommentPage
     * @param dynamicCommentPageSearchQo
     * @return
     */
    default IPage<DynamicComment> pageChildrenComment(Page<DynamicComment> dynamicCommentPage,
        DynamicCommentPageSearchQo dynamicCommentPageSearchQo) {
        LambdaQueryWrapper<DynamicComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DynamicComment::getParentUuid,
            dynamicCommentPageSearchQo.getDynamicCommentUuid());
        lambdaQueryWrapper.orderByDesc(DynamicComment::getCreateTime);
        return selectPage(dynamicCommentPage, lambdaQueryWrapper);
    }
}
